package com.et.install

import android.app.Service
import android.content.Intent
import android.os.Environment
import android.os.IBinder
import com.et.install.upgrade.IUpgradeAidl
import com.et.install.upgrade.IUpgradeCallBack
import com.et.logger.ELog

class InstallService : Service() {

    override fun onCreate() {
        super.onCreate()
        Environment.getExternalStoragePublicDirectory("log").apply {
            if (!exists()) mkdirs()
            if (exists()) {
                ELog.init(this)
            }
        }
    }

    override fun onBind(intent: Intent): IBinder {
        return BinderHolder()
    }

    class BinderHolder : IUpgradeAidl.Stub() {
        override fun install(pkg: String?, pathName: String?, callback: IUpgradeCallBack?) {
            val b = TaskInstall.getInstance().offerInstallTask(pkg, pathName, callback)
            if (!b) {
                callback?.upgradeResult(false, pkg, pathName)
            }
        }
    }

    override fun onRebind(intent: Intent?) {
        super.onRebind(intent)
    }

    override fun onUnbind(intent: Intent?): Boolean {
        return super.onUnbind(intent)
    }
}